﻿var value_ctl_id;
var display_ctl_id;
var dialogs = new Array();
var openers = new Array();
/*easeOutQuad,easeOutBounce,easeOutElastic,easeInOutElastic,easeInOutCirc,easeInOutBounce,easeInOutCirc*/
$j.fn.extend({
    showDialog: function(trigerId, flag) {
        var id = $j(this).attr('id');
        var dialogPackageId = '__' + id + '__';
        var jDialogPackageId = '#' + dialogPackageId;
        if ($j(jDialogPackageId).css('display') == 'none') {
            $j(jDialogPackageId).css({ position: 'absolute', 'z-index': '1000' });
            $j(jDialogPackageId).draggable({ handle: $j('.dialog_header') });
            var offset = $j('#' + trigerId).offset();
            $j(jDialogPackageId).css({ top: offset.top, left: offset.left });
            $j(jDialogPackageId).animate({
                opacity: 'show',
                top: ($j(document).height() - $(jDialogPackageId).height()) / 4,
                left: ($j(document).width() - $(jDialogPackageId).width()) / 2,
                height: 'toggle',
                width: 'toggle'
            }, 'show', 'easeOutQuad');
            $j(jDialogPackageId).attr('trigger', trigerId);
            value_ctl_id = trigerId.replace('_token', '_value');
            display_ctl_id = trigerId.replace('_token', '');
            if (1 == flag) {
                var closeId = id + '_close';
                closeAll(closeId);
                dialogs.push(closeId);
            }
            var _iframe = $j('#' + id + '_iframe');
            if (_iframe[0] != null) {
                if ($j(_iframe).attr('src') == null && $j(_iframe).attr('title') != null)
                    $j(_iframe).attr('src', $j(_iframe).attr('title'));
            }
        }
    },
    hideDialog: function() {
        var id = $j(this).attr('id').replace('_close', '');
        var dialogPackageId = '__' + id + '__';
        var jDialogPackageId = '#' + dialogPackageId;
        var triggerId = $j(jDialogPackageId).attr('trigger');
        if (triggerId != undefined) {
            var jTriggerId = '#' + triggerId;
            var offset = $j(jTriggerId).offset();
            $j(jDialogPackageId).animate({
                opacity: 'hide',
                top: offset.top,
                left: offset.left,
                height: 'toggle',
                width: 'toggle'
            }, 'hide', 'easeOutQuad');
            $j(jDialogPackageId).removeAttr('trigger');
        }
    },
    dialogCloseMouseEvent: function() {
        var cla = $j.trim($j(this).attr('class'));
        $j(this).mouseover(function() { $j(this).attr('class', cla + '-over'); });
        $j(this).mouseout(function() { $j(this).attr('class', cla); });
        $j(this).mousedown(function() {
            $j(this).hideDialog();
        });
    },
    /*showSelectorDialog*/open: function(url, feature_settings, v_control, d_control, flag) {
        var _setting = { scrollbars: 'yes',
            resizable: 'yes',
            width: '350',
            height: '500',
            location: 0,
            status: 0,
            toolbar: 0,
            menubar: 0
        };
        $j.extend(_setting, feature_settings);
        var l = ($j(document).width() - _setting.width) / 2;
        var t = ($j(document).height() - _setting.height) / 2;
        $j.extend(_setting, { top: t, left: l });
        var _feature = $j.param(_setting);
        var reg = new RegExp('&', "g");
        _feature = _feature.replace(reg, ',');
        value_ctl_id = v_control;
        display_ctl_id = d_control;
        selector = window.open(url, '_blank', _feature);
        selector.setReturnValue = setReturnValue;
        if (1 == flag) {
            closeAll(selector);
            openers.push(selector);
        }
    }
});

function setReturnValue(value, display) {
     if( value_ctl_id !=  null && value_ctl_id !=''){
         var value_obj;
         value_obj = document.getElementById(value_ctl_id);
       value_obj.value=value;
        if(value_obj.onchange!=null)
	       value_obj.onchange.call();
    }
    if( display_ctl_id != null && display_ctl_id!='') {
        var display_obj;
        display_obj = document.getElementById(display_ctl_id);
        display_obj.value = display;
        if(display_obj.onchange!=null)
	       display_obj.onchange.call();
}
closeAll();
}
  
  function openersClose(e) {
      if (openers.length > 0) {
          $j.each(openers, function(i, n) {
          if(n!=e)
              n.close();
          });
          openers.length = 0;
      }
  }  
  function dialogsClose(e)
  {
      if (dialogs.length > 0) {
          $j.each(dialogs, function(i, n) {
          if(n!=e)
              $j('#' + n).hideDialog();
          });
          dialogs.length = 0;
      }
  }
  function closeAll(e) {
      dialogsClose(e);
      openersClose(e);
  }

$j(function() {
$j('.dialog-close').dialogCloseMouseEvent();
});